package cn.lyl.org.controller;

import cn.lyl.basic.annotation.PreAuthorize;
import cn.lyl.org.domain.Department;
import cn.lyl.org.query.DepartmentQuery;
import cn.lyl.org.service.IDepartmentService;
import cn.lyl.basic.util.AjaxResult;
import cn.lyl.basic.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口:部门
 */
@Api(value = "部门接口类",description = "处理部门的CRUD操作")
@RestController
@RequestMapping("/department")
public class DepartController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查找一个
     * @return
     */
        //前端：get http://localhost:8080/department/1
    @ApiOperation(value = "查找单个对象")
    @GetMapping("/{id}")
    @PreAuthorize(name = "部门查找单个对象",sn = "department:getOne")
    public Department findOne(@PathVariable("id")Long id){
          return departmentService.findOne(id);
    }

    /**
     * 查找所有
     * @return
     */
    ////前端：get http://localhost:8080/department
    @ApiOperation(value = "查找所有")
    @GetMapping
    @PreAuthorize(name = "部门查找单个对象",sn = "department:getAll")
    public List<Department> findAll(){
           return departmentService.findAll();
    }

    /**
     * 删除功能
     * @param id
     * @return
     */
    //前端：delete http://localhost:8080/department/1
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "部门删除",sn = "department:delete")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或修改
     * @param department
     * @return
     */
    //前端：put http://localhost:8080/department
    @ApiOperation(value = "添加或修改")
    @PutMapping
    @PreAuthorize(name = "部门删除",sn = "department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId()==null){
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 分页查询或高级查询
     * @param departmentQuery
     * @return
     */
    ////前端：post http://localhost:8080/department json对象{}
    @ApiOperation(value = "分页查询或高级查询")
    @PostMapping
    @PreAuthorize(name = "部门删除",sn = "department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
           return departmentService.queryPage(departmentQuery);
    }

    @ApiOperation(value = "批量删除")
    @PatchMapping
    @PreAuthorize(name = "部门删除",sn = "department:patchDel")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    @ApiOperation(value = "接口：查询部门树")
    @GetMapping("/deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }


}
